//DBFile.java
import java.io.*;
import java.lang.Object;
import java.util.*;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.*;
import com.drew.metadata.exif.*;
import com.drew.metadata.jpeg.*;
import com.drew.metadata.iptc.*;

public class DBFile extends File {

   private String absPath = "";
   private String extension= "";
   private String keyWords = "";
   private String artist;
   private String genre;
   private String musicLength;
   private String dimension = "";
   private String director = "Steven Speilberg";
   private String flavor = "";
   private String album;
   private String title;
   private boolean markProtected = false;
   private String launchApp = null;
   private boolean masked = false;
   private String exifDateCreated;
   private String caption;
   private String dimensions;
   //private transient Mp3Info mp3Info;

  public DBFile(String absPath){
      super(absPath);
      this.absPath = absPath;
      setExt();
      if(extension.equals("jpg") || extension.equals("JPG")){
         File jpegFile = new File(absPath);
         setJpegData(jpegFile);
      }
      else if(extension.equals("mp3") || extension.equals("MP3")){
         mp3Info = new Mp3Info(this);
         mp3Info.setMp3();
      }
      setFlavor();
   }

  public void setJpegData(File jpegFile){
     try {
        Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
        Iterator it = metadata.getDirectoryIterator();
        Directory jpegDirectory = (JpegDirectory)it.next();
        JpegDescriptor jpeg = new JpegDescriptor(jpegDirectory);
        String height = jpeg.getImageHeightDescription();
        String width = jpeg.getImageWidthDescription();
           if(height != null && width != null){
              dimension = height+" x "+width;
           }
      }
      catch (Exception e) {
      }

  }


 public void setExt(){
     String absPath_nodir = this.getName();
     String ext = "";
    int idx = absPath_nodir.lastIndexOf(".");
      if (idx > 0)
        this.extension = absPath_nodir.substring(idx+1,absPath_nodir.length());
  }

   public String getExt(){
     return extension.toLowerCase();
   }

  public void setFlavor(){
    if (ExtensionKeys.musicFlavors.contains(this.extension)){
      this.flavor = ExtensionKeys.MUSICNAME;
    }
    if (ExtensionKeys.pictureFlavors.contains(this.extension)){
      this.flavor = ExtensionKeys.PICTURENAME;
    }
    if (ExtensionKeys.movieFlavors.contains(this.extension)){
      this.flavor = ExtensionKeys.MOVIENAME;
    }
  }

  public void mask(){
     masked = true;
  }

  public void unMask(){
     masked = false;
  }

  public boolean isMasked(){
     return masked;
  }

  public String getFlavor(){
    return flavor;
  }

   public String getArtist(){
       return artist;

   }

   public String getGenre(){
      return genre;
   }

   public String getTitle(){
      return title;
   }

   public void setTitle(){
      title = this.getName();
   }

   public void setGenre(String val){
            genre = val;
   }
   public void setArtist(String val){
         artist = val;
   }

   public void setTitle2(String val){
            title = val;
   }
   public void setAlbum(String val){
            album = val;
   }
   public void setMusicLength(String val){
            musicLength = val;
   }
   public String getMusicLength(){
      return musicLength;
   }

   public String getAlbum(){
      return album;
   }

   public String getDimension(){
      return dimension;
   }

   public String getDirector(){
      return director;
   }

   public String getAbsPath(){
       return absPath;
   }

   public void setAbsPath(String path){
          absPath = path;
   }

   public String getKeyWords(){
      return keyWords;
   }

   public boolean getMarkProtected(){
         return markProtected;
   }

   public void setMarkProtected(){
         markProtected = true;
   }

   public void appendToKeyWords(String str){
      String s = str.concat(" ");
      keyWords = keyWords.concat(s);
   }

   public boolean show(){
      if (this.isHidden()) return false;
      if (absPath.startsWith(".")) return false;
      if (absPath.startsWith("#")) return false;
      if (absPath.endsWith("#")) return false;
      if (absPath.endsWith("~")) return false;

      return true;
   }

   public void deleteFromKeyWords(String str){
      String result = "";
       String[] x = keyWords.split(" ");
      int len = x.length;
         for(int i=0; i<len; i++){
         String zz = (String)x[i];
             if(zz.equals(str)){}
             else{
                result = result.concat(zz);
                result = result.concat(" ");
             }
         }
      keyWords = result;
    }

   public String getLaunchApp(){
      return launchApp;
   }

   public void setLaunchApp(String app){
      launchApp = app;
   }
}
